<?php namespace App\Http\Controllers;

use App\Content;
use App\Goods;
use App\GoodsAttr;
use App\Orders;
use App\SysBanner;
use App\User;
use App\UserInfo;
use Auth;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }

    public function userList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Page.login'));

        $allUsers = User::orderby('id','desc')->paginate(20);

        if(count($allUsers) > 0){
            foreach ($allUsers as $user){
                $userinfo = UserInfo::whereUserId($user->id)->first();
                if($userinfo){
                    $user->userinfo = $userinfo;
                }else{
                    $user->userinfo = null;
                }
            }
        }

        return view('admin.user-list',['users'=>$allUsers]);
    }

    public function goodsList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Page.login'));

        $allGoods = Goods::orderBy('id','desc')->paginate(20);

        if(count($allGoods) > 0){
            foreach ($allGoods as $good){
                /** @var Goods $good */
                $goodsAttr = GoodsAttr::whereGoodId($good->id)->get();
                $good->goods_attrs = $goodsAttr;
            }
        }

        return view('admin.goods-list',['goods'=>$allGoods]);
    }

    public function addGoods(Request $request){
        $id = $request->get('id');

        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Page.login'));

        $thatGoods = Goods::whereId($id)->first();
        if($thatGoods){
            /** @var Goods $good */
            $goodsAttr = GoodsAttr::whereGoodId($thatGoods->id)->get();
            $thatGoods->goods_attrs = $goodsAttr;
        }

        return view('admin.add-goods',['goods'=>$thatGoods]);
    }

    public function orderList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Page.login'));

        $orders = Orders::orderBy('id','desc')->paginate(20);

        if(count($orders) > 0){
            foreach ($orders as $order){
                /** @var Orders $order */
                $thatGoods = Goods::whereId($order->good_id)->first();
                if($thatGoods){
                    $order->goods = $thatGoods;
                }else{
                    $order->goods = null;
                }
            }
        }

        return view('admin.order-list',['orders'=>$orders]);
    }

    public function bannerList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Page.login'));

        $sysBanners = SysBanner::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('admin.banner-list',['banners'=>$sysBanners]);
    }

    public function bannerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Page.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = SysBanner::whereId($id)->first();

        return view('admin.banner-set',['banner'=>$thatBanner]);
    }

    public function loginOut(){
        Auth::guard('admin')->logout();

        return redirect(route('Admin.Page.login'));
    }

    public function contentsList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $contents = Content::orderBy('created_at','desc')->get();

        return view('admin.contents-list',['contents' => $contents]);
    }

    public function contentsSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;


        $sysContents = Content::whereId($id)->first();

        return view('admin.contents-set',['sysContents' => $sysContents]);
    }

}